package com.daxue.lcm.api.grpc.client;

import com.daxue.lcm.api.grpc.config.GrpcServiceInfo;
import com.daxue.lcm.api.grpc.interfaces.dynamicloading.DynamicLoadingServiceGrpc.DynamicLoadingServiceBlockingStub;
import com.daxue.lcm.api.grpc.interfaces.models.CommonParams.ModelReadByCodeParams;
import com.daxue.lcm.api.grpc.model.DynamicResourceConfig;
import com.daxue.lcm.api.grpc.utis.GrpcClientUtils;
import io.grpc.Channel;
import org.springframework.lang.NonNull;

import static com.daxue.lcm.api.grpc.interfaces.dynamicloading.DynamicLoadingServiceGrpc.newBlockingStub;

/**
 * @author daxue0929
 * @date 2022/9/28
 */

public class DynamicLoadingGrpcServiceClient extends AbstractGrpcServiceClient<DynamicLoadingServiceBlockingStub> {

    public static final String SERVICE_NAME = "dynamic-loading";

    public DynamicLoadingGrpcServiceClient(Channel channel, GrpcServiceInfo serviceInfo) {
        super(channel, serviceInfo);
    }

    @Override
    protected DynamicLoadingServiceBlockingStub genStub(Channel channel) {
        return newBlockingStub(channel);
    }


    public DynamicResourceConfig configReadByCode(
        @NonNull final String tenantId, @NonNull final String code
    ) throws Exception {
        ModelReadByCodeParams params = ModelReadByCodeParams.newBuilder()
            .setTenantId(tenantId).setCode(code).build();

        DynamicResourceConfig dynamicResourceConfig =
            GrpcClientUtils.extractResponseBody(params, r -> getStub().configReadByCode(r),
                DynamicResourceConfig.class
            );
        return dynamicResourceConfig;
    }


}
